//
//  GoodsListViewController.m
//  收纳
//
//  Created by  on 2022/10/28.
//

#import "GoodsListViewController.h"
#import "ListCell.h"

@interface GoodsListViewController ()
@property (strong, nonatomic) IBOutlet UILabel *num;
@property (strong, nonatomic) IBOutlet UILabel *money;
@property (strong, nonatomic) IBOutlet UIView *cycle;
@property (strong, nonatomic) IBOutlet UILabel *totalNum;
@property (strong, nonatomic) IBOutlet UITableView *table;
@property (nonatomic, strong) NSArray <Goods*>*array;
@property (nonatomic, strong) NSArray <Goods*>*subArray;
@property (nonatomic, strong) NSMutableArray *colors;
@property (nonatomic, strong) NSMutableArray *numbers;

@end

@implementation GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}
- (void)initUI{
    self.title = LS(@"统计");
    [self.table registerNib:[ListCell nib] forCellReuseIdentifier:[ListCell ID]];
}
- (void)loadData{
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [GoodsTool find:YES complete:^(NSMutableArray<Goods *> * _Nonnull result) {
            self.array = result;
            dispatch_group_leave(group);
            
        }];
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [GoodsTool find:NO complete:^(NSMutableArray<Goods *> * _Nonnull result) {
            self.subArray = result;
            dispatch_group_leave(group);
            
        }];
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        int money = 0;
        for (int i = 0; i<self.subArray.count; i++) {
            Goods *model =  self.subArray[i];
            money += [model.money intValue];
        }
        
        int total_count = 0;
        self.colors = [NSMutableArray array];
        self.numbers = [NSMutableArray array];
        for (int i = 0; i<self.array.count; i++) {
            Goods *model =  self.array[i];
            [self.colors addObject:[self nextHex:@"#F9C1AD" count:i]];
            
            NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"isContainer"),bg_sqlValue(@(NO)),bg_sqlKey(@"container"),bg_sqlValue(model.name)];
            NSInteger count = [Goods bg_count:nil where:where];
            total_count +=  count;
            [self.numbers addObject:@(count)];
        }
        
        CGFloat start = M_PI * 3 / 2;
        for (int  i = 0; i<self.numbers.count; i++) {
            int num = [self.numbers[i] intValue];
            NSString *hex = self.colors[i];
            
            CGFloat end = start +  M_PI * 2 * num / total_count;
            [self setupLayer:hex start:start end:end];
            start = end;
        }
        
        self.money.text = [NSString stringWithFormat:@"¥%d",money];
        self.num.text = [NSString stringWithFormat:@"%d",total_count];
        self.totalNum.text = self.num.text;
        [self.table reloadData];
    });
}
- (NSString *)nextHex:(NSString*)hex count:(NSInteger)count{
    NSString *str = [hex substringFromIndex:1];
    NSInteger num  = [str toDemical] - 25700 * count;
    NSString *newnum = [NSString stringWithFormat:@"%ld",num];
    NSString *newhex = [NSString stringWithFormat:@"#%@",[newnum toHex]];
    return newhex;
}
- (void)setupLayer:(NSString *)hexcolor start:(CGFloat)start end:(CGFloat)end{
    CGFloat r = self.cycle.bounds.size.width / 2;
    CGRect bounds = CGRectMake(0, 0, 2*r, 2*r);
    CAShapeLayer *layer = [CAShapeLayer  layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(r, r) radius:r startAngle:start endAngle:end  clockwise:YES];
    layer.frame = bounds;
    layer.lineWidth = 20;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = Hex(hexcolor).CGColor;
    layer.path = path.CGPath;
    layer.strokeEnd = 1;
    [self.cycle.layer addSublayer:layer];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:[ListCell ID]];
    Goods *model = self.array[indexPath.row];
    cell.title.text = model.name;
    cell.content.text = [NSString stringWithFormat:@"%@",self.numbers[indexPath.row]];
    cell.color.bgHexColor = self.colors[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
@end

